package ntu.metaweb.http;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;

import ntu.metaweb.Metaweb;
import ntu.metaweb.storage.WebPageQueryKey;
import ntu.metaweb.update.DeleteLink;
import ntu.p2p.dht.DHTContent;
import ntu.p2p.dht.messaging.DHTUpdateObjectMessage;
import rice.Continuation;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;

@SuppressWarnings("restriction")
public class DeleteLinkHandler implements CommandHandler {

	@Override
	public void handleCommand(final HttpExchange exchange,
			final HashMap<String, String> params) throws IOException {
		if (params.get("from_url_parameter").equals("null"))
			params.put("from_url_parameter", "");
		if (params.get("to_url_parameter").equals("null"))
			params.put("to_url_parameter", "");
		final WebPageQueryKey fromKey = new WebPageQueryKey(
				URLDecoder.decode(params.get("from_url"), "UTF-8"), 
				URLDecoder.decode(params.get("from_url_parameter"), "UTF-8"));
		final URL fromUrl = new URL(URLDecoder.decode(
				params.get("from_url") + params.get("from_url_parameter"), "UTF-8"));
		final WebPageQueryKey toKey = new WebPageQueryKey(
				URLDecoder.decode(params.get("to_url"), "UTF-8"), 
				URLDecoder.decode(params.get("to_url_parameter"), "UTF-8"));
		final URL toUrl = new URL(URLDecoder.decode(
				params.get("to_url") + params.get("to_url_parameter"), "UTF-8"));
		final DeleteLink deleteLink = new DeleteLink(fromUrl, toUrl, params.get("username"));
		
		Metaweb.getInstance().getDhtApplication().lookup(fromKey, 
				new Continuation<DHTContent,Exception>() {

					@Override
					public void receiveException(Exception exception) {

					}

					@Override
					public void receiveResult(DHTContent result) {
						DHTUpdateObjectMessage updateMessage = 
							new DHTUpdateObjectMessage(fromKey, deleteLink);
						Metaweb.getInstance().getDhtApplication().update(fromKey, updateMessage, 
								new Continuation<Object, Exception>() {

									@Override
									public void receiveException(Exception exception) {
										// TODO Auto-generated method stub
										
									}

									@Override
									public void receiveResult(Object result) {
										System.out.println("update successs");
										onReceiveResult(exchange, result);
									}
						});
					}
		});
		
		Metaweb.getInstance().getDhtApplication().lookup(toKey, 
				new Continuation<DHTContent,Exception>() {

					@Override
					public void receiveException(Exception exception) {

					}

					@Override
					public void receiveResult(DHTContent result) {
						DHTUpdateObjectMessage updateMessage = 
							new DHTUpdateObjectMessage(toKey, deleteLink);
						Metaweb.getInstance().getDhtApplication().update(toKey, updateMessage, 
								new Continuation<Object, Exception>() {

									@Override
									public void receiveException(Exception exception) {
										// TODO Auto-generated method stub
										
									}

									@Override
									public void receiveResult(Object result) {
										System.out.println("update successs");
									}
						});
					}
		});
	}

	protected void onReceiveResult(HttpExchange exchange, Object result) {
		try {
			Headers responseHeaders = exchange.getResponseHeaders();
			responseHeaders.set("Content-Type", "text/xml");
			exchange.sendResponseHeaders(200, 0);

			OutputStream responseBody = exchange.getResponseBody();
			String temp = "<xmlresponse>\n" 
					+ "<returncode>1</returncode>\n"
					+ "</xmlresponse>";
			responseBody.write(temp.getBytes());
			responseBody.close();
			exchange.close();
			System.out.println("close delete tag handler");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			exchange.close();
			System.out.println("close delete tag handler");
		}
	}

}